package com.tyun.modules.sys.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.tyun.common.utils.R;
import com.tyun.modules.sys.entity.SysUser;
import com.tyun.modules.sys.service.ISysUserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author taocongcong
 * @create 2017-12-15 13:50
 */
@RestController
@RequestMapping("sysUser")
public class SysUserController extends CommonController{

    private static final Logger logger = LogManager.getLogger(SysUserController.class);

    @Autowired
    private ISysUserService sysUserService;

    @GetMapping("/list")
    @Cacheable(value = "user-list-key")
    public List<SysUser> list(){
        SysUser sysUser=new SysUser();
        List<SysUser> sysUserList = sysUserService.selectList(new EntityWrapper<>(sysUser));
        logger.info("在" + new Date() + "查询所有用户");
        return sysUserList;
    }

    @GetMapping("/view")
    @Cacheable(value = "user-key",key="#userid+'view'")
    public SysUser view(String userid){
        SysUser sysUser = sysUserService.selectById(userid);
        logger.info("在" + new Date() + "查询用户，id：" + userid);
        return sysUser;
    }

    @GetMapping("/info")
    public R info(){
        SysUser sysUser = sysUserService.selectById(getUserId());
        logger.info("在" + new Date() + "查询当前用户");
        return R.ok().put("user", sysUser);
    }
}
